package com.itheima.test21;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class test {
    public static void main(String[] args) throws IOException {
        TreeSet<Student> treeSet = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int num = o2.sum() - o1.sum();
                int num1 = (num == 0 ? o2.getChineseScore() - o1.getChineseScore() : num);
                int num2 = (num1 == 0 ? o2.getMathScore() - o1.getMathScore() : num1);
                int num3 = (num2 == 0 ? o2.getEnglishScore() - o1.getEnglishScore() : num2);
                return num3;
            }
        });

        // 录入成绩
        for (int i = 1; i <= 3; i++) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请录入第" + i + "位学生信息");
            System.out.println("姓名:");
            String s = sc.nextLine();
            System.out.println("语文成绩:");
            int a = sc.nextInt();
            System.out.println("数学成绩:");
            int b = sc.nextInt();
            System.out.println("英语成绩:");
            int c = sc.nextInt();
            Student sd = new Student();
            sd.setName(s);
            sd.setChineseScore(a);
            sd.setMathScore(b);
            sd.setEnglishScore(c);
            treeSet.add(sd);
        }

        // 把集合添加到文本上
        BufferedWriter writer = new BufferedWriter(new FileWriter("test03\\src\\com\\itheima\\test21\\student.txt"));
        writer.write("学生信息如下:");
        writer.newLine();
        writer.write("姓名\t" + "语文\t" + "数学\t" + "英语");
        writer.newLine();
        for (Student s : treeSet) {
            StringBuilder stb = new StringBuilder();
            stb.append(s.getName()).append("\t").append(s.getChineseScore()).append("\t\t").append(s.getMathScore()).append("\t\t").append(s.getEnglishScore());
            writer.write(stb.toString());
            writer.newLine();
            writer.flush();
        }
        writer.close();
        System.out.println("数据写入文件完毕");
    }
}
